﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ControlLayer;

namespace GUI
{
    public partial class frmTiepNhanHoSoDangKi : Form
    {
        private DKDoiBongControl control;//Sử lý xự kiện
        public frmTiepNhanHoSoDangKi()
        {
            InitializeComponent();

            control = new DKDoiBongControl();

            //Chuyền dữ liệu vào comboBox trong DataGridView
            (dataGridView_DSCT.Columns["LoaiCauThu"] as DataGridViewComboBoxColumn).DataSource = control.BDSTenLoaiCauThu();
            (dataGridView_DSCT.Columns["LoaiCauThu"] as DataGridViewComboBoxColumn).DisplayMember = "TenLoaiCauThu";
            (dataGridView_DSCT.Columns["LoaiCauThu"] as DataGridViewComboBoxColumn).ValueMember = "MaLoaiCauThu";            
        }

        private void frmTiepNhanHoSoDangKi_Load(object sender, EventArgs e)
        {
            textBox_TenDB.Clear();
            textBox_TenSB.Clear();
            for (int i = 0; dataGridView_DSCT.RowCount > 1; i++)
            {
                dataGridView_DSCT.Rows.RemoveAt(0);
            }
        }

        #region Mở tập tin excel
        //
        private void MoFile_Click(object sender, EventArgs e)
        {
            openFileExcel.ShowDialog();
        }

        private void openFileExcel_FileOk(object sender, CancelEventArgs e)
        {
            string TenTapTin = openFileExcel.SafeFileName;
            textBox_TenDB.Text = control.TenDoiBong(TenTapTin);
            string TenBang = control.OpenFile_OK(dataGridView_DSCT, openFileExcel.FileName);
            textBox_TenSB.Text = control.TenSanBong(TenBang);           
            
        }
        //
        #endregion

        #region Đăng ký đội bóng
        //
        private void DangKy_Click(object sender, EventArgs e)
        {
            if (control.DangKi_Click(dataGridView_DSCT))
            {
                string maSanBong = control.InsertSanBong(textBox_TenSB.Text);
                string maDoiBong = control.InsertDoiBong(textBox_TenDB.Text, maSanBong);

                foreach (DataGridViewRow row in dataGridView_DSCT.Rows)
                {
                    if(row.Index == dataGridView_DSCT.RowCount - 1 )
                    {
                        break;
                    }
                    control.InsertCauThu(row.Cells["TenCauThu"].Value.ToString(), 
                                         maDoiBong, 
                                         row.Cells["NgaySinh"].Value.ToString(), 
                                         row.Cells["LoaiCauThu"].Value.ToString(), 
                                         row.Cells["GhiChu"].Value.ToString());
                }
                frmTiepNhanHoSoDangKi_Load(null, null);
            }
        }        
        //
        #endregion

        #region Đóng form lại
        //
        private void Dong_Click(object sender, EventArgs e)
        {
            this.Close();
        }        
        //
        #endregion
        
        private void Xoa_Click(object sender, EventArgs e)
        {
            frmTiepNhanHoSoDangKi_Load(null, null);
        }
    }
}
